package org.wangbao.web;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;
import org.junit.Test;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class BuildSwfAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public String templateName;

	public String xmlName;

	private File uploadSwf;

	private String swfName = "test.swf";

	private File uploadXml;

	private File uploadTemplate;

	String cmds = "notepad";
	
	String path;

	public String getTemplateName() {
		return templateName;
	}

	public void setTemplateName(String templateName) {
		this.templateName = templateName;
	}

	public String getXmlName() {
		return xmlName;
	}

	public void setXmlName(String xmlName) {
		this.xmlName = xmlName;
	}

	public String getSwfName() {
		return swfName;
	}

	public void setSwfName(String swfName) {
		this.swfName = swfName;
	}

	public String execute() {

		try {

			InputStream streamIn = new BufferedInputStream(new FileInputStream(
					uploadSwf));

			ActionContext ac = ActionContext.getContext();

			ServletContext sc = (ServletContext) ac
					.get(ServletActionContext.SERVLET_CONTEXT);

			path = sc.getRealPath("/");

			OutputStream streamOut = new FileOutputStream(path + "/swfName");

			int byteRead = 0;

			byte[] buffer = new byte[8192];

			while ((byteRead = streamIn.read(buffer, 0, 8192)) != -1) {
				streamOut.write(buffer, 0, byteRead);
			}

			System.out.println("upload is over.." + path);
			
			executeCommand();

			streamOut.close();
			streamIn.close();

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return "success";

	}

	@Test
	public void executeCommand() {
		try {
			Process ps = Runtime.getRuntime().exec(cmds+" "+ path + swfName );
			System.out.print(loadStream(ps.getInputStream()));
			System.err.print(loadStream(ps.getErrorStream()));
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
	}

	// read an input-stream into a String
	String loadStream(InputStream in) throws IOException {
		int ptr = 0;
		in = new BufferedInputStream(in);
		StringBuffer buffer = new StringBuffer();
		while ((ptr = in.read()) != -1) {
			buffer.append((char) ptr);
		}
		return buffer.toString();
	}


	public File getUploadSwf() {
		return uploadSwf;
	}

	public void setUploadSwf(File uploadSwf) {
		this.uploadSwf = uploadSwf;
	}

	public File getUploadXml() {
		return uploadXml;
	}

	public void setUploadXml(File uploadXml) {
		this.uploadXml = uploadXml;
	}

	public File getUploadTemplate() {
		return uploadTemplate;
	}

	public void setUploadTemplate(File uploadTemplate) {
		this.uploadTemplate = uploadTemplate;
	}

	public String getCmds() {
		return cmds;
	}

	public void setCmds(String cmds) {
		this.cmds = cmds;
	}

	public static long getSerialVersionUID() {
		return serialVersionUID;
	}

}
